package com.fowo.api.sys.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fowo.api.sys.entity.SysFile;
import com.fowo.api.sys.mapper.SysFileMapper;
import com.fowo.api.sys.model.CreateSysFile;
import com.fowo.api.sys.model.SysFileItemVo;
import com.fowo.api.sys.model.SysFileSearch;
import com.fowo.api.sys.service.SysFileService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

@Service
public class SysFileServiceImpl extends ServiceImpl<SysFileMapper, SysFile> implements SysFileService {
    @Override
    public Page<SysFileItemVo> searchPage(SysFileSearch search) {
        return baseMapper.searchPage(search.toPage(), search);
    }

    @Override
    public SysFile create(CreateSysFile model) {
        SysFile sysFile = new CreateSysFile();
        BeanUtils.copyProperties(model, sysFile);
        sysFile.doCreate();
        baseMapper.insert(sysFile);
        return sysFile;
    }

    @Override
    public SysFile getByStorageKey(String storageKey) {
        return baseMapper.selectOne(Wrappers.<SysFile>query().eq("storage_key", storageKey));
    }

    @Override
    public SysFile getByFilename(String filename) {
        return baseMapper.selectLastByFilename(filename);
    }
}
